package ua.kpi_java_training.zastupaylo.module1;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * This class includes methods for finding all palindromes in a string.
 *
 * @author Zastupaylo Alexander (zastupaylo@gmail.com)
 * @version 1.4, 24/05/2013
 */
public class PalindromeFinder {

    public static final int MIN_PALINDROME_LENGTH = 3;
    public static ArrayList<String> palindromes = new ArrayList<String>();

    private static Scanner scanner = new Scanner( System.in );

    /**
     * The main method for the PalindromeFinder program.
     *
     * @param args - Not used
     */
    public static void main(String[] args) {
        String inputPhrase = input();
        findPalindrome(inputPhrase);
        output();
    }

    /**
     * Read phrase from the command line.
     *
     * @return inputted phrase
     * @see String
     */
    public static String input(){
        String inputPhrase = "";
        System.out.println("Enter phrase:");
        while (true){
            inputPhrase = scanner.nextLine();
            if (inputPhrase.length() >= MIN_PALINDROME_LENGTH){
                scanner.close();
                break;
            }
            System.out.println("Input phrase is too short. The length of the phrase should be more than 2.");
        }
        return inputPhrase;
    }

    /**
     * Find all palindromes in the string.
     * Found palindromes are added to <code>PalindromeFinder</code>.palindromes.
     *
     * @param inputPhrase - string to find palindromes in
     * @see String
     */
    public static void findPalindrome(String inputPhrase){

        for (int palindromeLength = MIN_PALINDROME_LENGTH; palindromeLength <= inputPhrase.length(); palindromeLength++){
            findSpecifiedLengthPalindromes(palindromeLength, inputPhrase);
            }
    }

    /**
     * Find all palindromes of particular length.
     * Found palindromes are added to <code>PalindromeFinder</code>.palindromes.
     *
     * @param palindromeLength - palindrome length
     * @param inputPhrase - string to find palindromes in
     */
    public static void findSpecifiedLengthPalindromes(int palindromeLength, String inputPhrase){
        for (int sublineBeginning = 0; sublineBeginning + palindromeLength <= inputPhrase.length(); sublineBeginning++){
            if (isPalindrome(inputPhrase.substring(sublineBeginning, sublineBeginning + palindromeLength))) {
                palindromes.add(inputPhrase.substring(sublineBeginning, sublineBeginning + palindromeLength));
            }
        }
    }

    /**
     * Is word a palindrome
     *
     * @param word - string to check for a palindrome status
     * @return <code>true</code> if word is a palindrome, otherwise - <code>false</code>
     */
    public static boolean isPalindrome(String word){
        return word.equals(new StringBuffer(word).reverse().toString());
    }

    /**
     * Print result of searching for palindromes to command line.
     */
    public static void output(){
        if (palindromes.isEmpty()){
            System.out.println("No palindromes found.");
        }
        else{
            System.out.println("List of palindromes:");
            for(String element : palindromes){
                System.out.println(element);
            }
        }
    }
}
